package com.guigu.aggregation.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.aggregation.DTO.DriverJobQueryDTO;
import com.guigu.aggregation.DTO.TaskPickupDispatchQueryDTO;
import com.guigu.aggregation.DTO.TaskTransportQueryDTO;
import com.guigu.aggregation.DTO.TransportOrderQueryDTO;
import com.guigu.aggregation.DTO.work.DriverJobDTO;
import com.guigu.aggregation.DTO.work.TaskPickupDispatchDTO;
import com.guigu.aggregation.DTO.work.TaskTransportDTO;
import com.guigu.aggregation.DTO.work.TransportOrderDTO;
import com.guigu.aggregation.entity.Order;
import com.guigu.aggregation.mapper.WebManagerMapper;
import com.guigu.aggregation.service.OrderService;
import com.guigu.aggregation.service.WebManagerService;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WebManagerServiceImpl implements WebManagerService {

    @Autowired
    private WebManagerMapper webManagerMapper;

    @Autowired(required = false)
    private OrderService orderService;

    @Override
    public R<DriverJobDTO> findDriverJobByPage(DriverJobQueryDTO dto) {
        IPage<DriverJobDTO> iPage = new Page();
        iPage.setSize(dto.getPageSize());
        iPage.setCurrent(dto.getPage());
        IPage<DriverJobDTO> page = webManagerMapper.findDriverJobByPage(iPage, dto);
        return new R(ResultCode.SUCCESS_CODE,"查询成功!",page);
    }

    @Override
    public R<TaskPickupDispatchDTO> findTaskPickupDispatchJobByPage(TaskPickupDispatchQueryDTO dto) {
        IPage<TaskPickupDispatchDTO> iPage = new Page();
        iPage.setSize(dto.getPageSize());
        iPage.setCurrent(dto.getPage());
        IPage<TaskPickupDispatchDTO> page = webManagerMapper.findTaskPickupDispatchJobByPage(iPage, dto);
        return new R(ResultCode.SUCCESS_CODE,"查询成功!",page);
    }

    @Override
    public R<TransportOrderDTO> findTransportOrderByPage(TransportOrderQueryDTO dto) {
        System.out.println("dto = " + dto);
        IPage<TransportOrderDTO> iPage = new Page();
        iPage.setSize(dto.getPageSize());
        iPage.setCurrent(dto.getPage());
        IPage<TransportOrderDTO> page = webManagerMapper.findTransportOrderByPage(iPage, dto);
        //添加对应订单信息
        List<TransportOrderDTO> records = page.getRecords();
        for(TransportOrderDTO transportOrderDTO:records){
            Order order = this.orderService.getById(transportOrderDTO.getOrderId());
            transportOrderDTO.setOrder(order);
        }
        //设置
        page.setRecords(records);
        return new R(ResultCode.SUCCESS_CODE,"查询成功!",page);
    }

    @Override
    public R<TaskTransportDTO> findTaskTransportByPage(TaskTransportQueryDTO dto) {
        IPage<TaskTransportDTO> iPage = new Page();
        iPage.setSize(dto.getPageSize());
        iPage.setCurrent(dto.getPage());
        IPage<TaskTransportDTO> page = webManagerMapper.findTaskTransportByPage(iPage, dto);
        return new R(ResultCode.SUCCESS_CODE,"查询成功!",page);
    }
}
